package com.cheyun.netsalev3.view.orderlist


import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.RecyclerView
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.MyPagedListAdapter
import com.cheyun.netsalev3.bean.order.OrderData
import com.cheyun.netsalev3.databinding.FragmentOrderListBinding
import com.cheyun.netsalev3.utils.BaseFragment
import com.cheyun.netsalev3.utils.api.NetworkState
import com.cheyun.netsalev3.viewmodel.orderlist.OrderListFragmentViewModel
import com.jeremyliao.liveeventbus.LiveEventBus

/**
 * 已到店 未到店
 */
class OrderListFragment : BaseFragment<FragmentOrderListBinding>() {
    override fun initContentView(): Int {
        return R.layout.fragment_order_list
    }

    override fun initViewModel(): ViewModel? {
        activity?.apply {
            return ViewModelProviders.of(this).get(OrderListFragmentViewModel::class.java)
        }
        return null
    }

    override fun initData() {
        binding.viewModel!!.pagedlist[nkey]?.observe(this, Observer { list ->
            Log.e("pagedlist....", list.toString())
            list?.apply {
                adapter?.submitList(this)
            }
        })
        binding.viewModel!!.hashMap[nkey]?.observe(this, Observer {
            binding.swipeRefreshLayout.isRefreshing = it == NetworkState.LOADING
            adapter?.setNetworkState(it)
        })
    }

    fun setSearch(hashMap:HashMap<String, String>){
        binding.viewModel?.refresh(nkey,hashMap)
    }

    private var nkey: String = ""
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            nkey = it.getString("nkey")
        }
    }

    override fun lazyInit() {
        Log.i(TAG,"override lazyinit")
        binding.viewModel?.initData(nkey)
    }

    var adapter: MyPagedListAdapter<OrderData>?=null


    override fun initView() {
        adapter = MyPagedListAdapter(R.layout.orderlist_item_layout, binding.viewModel)
        var recyclerView= binding.root.findViewById<RecyclerView>(R.id.swipe_target)
        recyclerView.adapter = adapter
        binding.swipeRefreshLayout.setOnRefreshListener {
            binding.viewModel?.refresh(nkey)
        }
        //通知更新
        LiveEventBus.get()
                .with("CustomerItemFragment", String::class.java)
                .observe(this, Observer<String> {
                    binding.viewModel?.refresh(nkey)
                })
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (resultCode == 101) {
            adapter?.notifyDataSetChanged()
        }
    }

    companion object {
        @JvmStatic
        fun newInstance(nkey: String) =
                OrderListFragment().apply {
                    arguments = Bundle().apply {
                        putString("nkey", nkey)
                    }
                }
    }
}
